(function() {
    angular.module('app').component('wordsControllerComponent', {
        templateUrl: 'app/controllers/words.controller/words.controller.component/words.controller.component.html',
        controller: wordsControllerComponent,
        controllerAs: 'vm',
        bindings: {
            seen: '='
        }
    });

    wordsControllerComponent.$inject = ['wordsControllerComponentService', '$rootScope', '$scope', '$loading', 'util'];

    function wordsControllerComponent(wordsControllerComponentService, $rootScope, $scope, $loading, util) {

        var vm = this;

        vm.word = '';

        vm.tabSeen = false; //店铺排行榜商品tab


        vm.option = {
            title: {
                text: '',
                x: 'center'
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: { // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
                },
                formatter: function(params) {
                    var str = '<div style = "width:120px;text-align: left;">' + params[0].name + '</div>';

                    for (var i = 0; i < params.length; i++) {
                        str += '<div style = "width:120px;text-align: left;padding-left:10px;">' + params[i].seriesName + ' : ' + util.bigNumFormat(params[i].data) + '</div>';
                    }

                    return str;
                }
            },
            grid: {
                left: 40
            },
            legend: {
                data: ['客单价', '热门指数'],
                x: 25,
                y: 15
            },
            xAxis: {
                data: [],
                axisLabel: {
                    //rotate: 50,
                    show: true
                }
            },
            yAxis: [{
                type: 'value',
                axisLabel: {
                    formatter: function(value, index) {
                        var len = String(value).length;

                        if (value < 10000) {
                            return value;
                        }

                        switch (len) {
                            case 5:
                                return (value / 10000).toFixed(2) + '万';
                            case 6:
                                return (value / 10000).toFixed(2) + '万';
                            case 7:
                                return (value / 10000).toFixed(2) + '万';
                            case 8:
                                return (value / 10000).toFixed(2) + '万';
                            case 9:
                                return (value / 100000000).toFixed(2) + '亿';
                            case 10:
                                return (value / 100000000).toFixed(2) + '亿';
                            case 11:
                                return (value / 100000000).toFixed(2) + '亿';
                            case 12:
                                return (value / 100000000).toFixed(2) + '亿';
                        }

                        return value;
                    }
                }
            }],
            series: [
                // {
                //     name: '销售额',
                //     type: 'line',
                //     data: data
                // },
                {
                    name: '客单价',
                    type: 'line',
                    data: []
                }, {
                    name: '热门指数',
                    type: 'line',
                    data: [],
                    label: {
                        normal: {
                            formatter: function(value) {
                                return '元';
                            }
                        },
                        emphasis: function(value) {
                            return '33';
                        }
                    }
                }
            ]
        };

        vm.topProds = [];

        vm.topShops = [];

        vm.title = "";

        vm.getShopDetail = _getShopDetail; //function(shop)

        var clearFun = $rootScope.$on('words_controller_component_EV', function(e, data, word) {

            vm.word = word;
            if (data.type == 'hot') {
                vm.option.legend.data = ['客单价', '热门指数'];
                vm.option.series[1].name = "热门指数";
                vm.title = "热门指数及客单价走势";
            } else {
                vm.option.legend.data = ['客单价', '增长指数'];
                vm.option.series[1].name = "增长指数";
                vm.title = "增长指数及客单价走势";
            }



            _init(data, word);
        });

        $scope.$on('$destroy', function() {
            clearFun();
        });


        function _init(data, word) {

            var tableJson = {
                category: data.category,
                word: word
            };

            var lineJson = angular.extend({}, data, { word: word });

            _getLine(lineJson);

            _getTopShop(tableJson);

            _getTopProduct(tableJson);

        }

        function _getLine(lineJson) {

            $rootScope.$emit('echart_draw_line_loading_EV', 'words_line');
            wordsControllerComponentService.getLineData(lineJson).then(function(data) {

                var option = angular.copy(vm.option);

                for (var i = 0; i < data.length; i++) {
                    option.xAxis.data.push(data[i].date.split(' ')[0]);
                    // option.series[0].data.push(Number(data[i].sellMoney));

                    if (data[i].sellAmount == 0) {
                        option.series[0].data.push(0);
                    } else {
                        option.series[0].data.push(Number(data[i].sellMoney / data[i].sellAmount));
                    }

                    if (lineJson.type == 'hot') {
                        option.series[1].data.push(data[i].hotIndexNumber);

                    } else {
                        option.series[1].data.push(data[i].growthIndexNumber);
                    }
                }




                $rootScope.$emit('echart_draw_line_EV', option, 'words_line');

            }, function(err) {});
        }

        function _getTopShop(tableJson) {


            $loading.start('wordsTopShops');
            wordsControllerComponentService.getTopShop(tableJson).then(function(data) {

                vm.topShops.length = 0;

                vm.topShops = data;

                $loading.finish('wordsTopShops');

            }, function(err) {
                $loading.finish('wordsTopShops');
            });
        }


        function _getTopProduct(tableJson) {
            $loading.start('wordsTopProds');
            wordsControllerComponentService.getTopProduct(tableJson).then(function(data) {

                vm.topProds.length = 0;

                vm.topProds = data;

                $loading.finish('wordsTopProds');
            }, function(err) { $loading.finish('wordsTopProds'); });
        }


        function _getShopDetail(shop, shopName) {
            $rootScope.$emit('formShop_detail_EV', shop, shopName)
        }
    }

})();
